/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Flogin.java
 *
 * Created on 2 mai 2011, 15:12:15
 */

package frame;

import client.Client;
import client.ClientOdImpl;
import java.awt.Dimension;
import java.awt.Toolkit;
import javax.swing.JOptionPane;
import server._ZoneExchange;
import user.Login;

/**
 *
 * @author qiaob
 */
public class Flogin extends javax.swing.JFrame {

    /** Creates new form Flogin */
    public Flogin() {
        initComponents();
        this.setCenter();
        this.pConfig.setVisible(false);
        this.client=new Client();
        this.textPort.setText(client.getPort());
        this.textIp.setText(client.getHostName());        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pLogin = new javax.swing.JPanel();
        labelId = new javax.swing.JLabel();
        labelPwd = new javax.swing.JLabel();
        textId = new javax.swing.JTextField();
        textPwd = new javax.swing.JPasswordField();
        pTitle = new javax.swing.JPanel();
        labelTitle = new javax.swing.JLabel();
        pConfig = new javax.swing.JPanel();
        labelServer = new javax.swing.JLabel();
        textIp = new javax.swing.JTextField();
        labelPort = new javax.swing.JLabel();
        textPort = new javax.swing.JTextField();
        bConfig = new javax.swing.JButton();
        bRegistre = new javax.swing.JButton();
        bLogin = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        setBackground(new java.awt.Color(225, 245, 252));
        setBounds(new java.awt.Rectangle(0, 0, 330, 240));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setForeground(new java.awt.Color(225, 245, 252));
        setName("Venus"); // NOI18N
        setResizable(false);

        pLogin.setBackground(new java.awt.Color(241, 250, 255));
        pLogin.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(125, 220, 245)));
        pLogin.setPreferredSize(new java.awt.Dimension(310, 110));
        pLogin.setLayout(null);

        labelId.setBackground(new java.awt.Color(13, 55, 85));
        labelId.setForeground(new java.awt.Color(13, 55, 85));
        labelId.setText("Login :");
        pLogin.add(labelId);
        labelId.setBounds(10, 20, 100, 20);

        labelPwd.setForeground(new java.awt.Color(13, 55, 85));
        labelPwd.setText("Mot de pass :");
        pLogin.add(labelPwd);
        labelPwd.setBounds(10, 60, 100, 20);

        textId.setColumns(25);
        pLogin.add(textId);
        textId.setBounds(100, 20, 150, 20);

        textPwd.setColumns(25);
        pLogin.add(textPwd);
        textPwd.setBounds(100, 60, 150, 20);

        pTitle.setBackground(new java.awt.Color(153, 153, 255));
        pTitle.setLayout(null);

        labelTitle.setFont(new java.awt.Font("Tahoma", 0, 36));
        labelTitle.setText("Venus");
        pTitle.add(labelTitle);
        labelTitle.setBounds(100, 10, 120, 30);

        pConfig.setBackground(new java.awt.Color(225, 245, 252));
        pConfig.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(125, 220, 245)));
        pConfig.setPreferredSize(new java.awt.Dimension(310, 30));
        pConfig.setLayout(null);

        labelServer.setText("Adresse  Server:");
        pConfig.add(labelServer);
        labelServer.setBounds(5, 5, 80, 20);

        textIp.setColumns(15);
        textIp.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                textIpFocusLost(evt);
            }
        });
        pConfig.add(textIp);
        textIp.setBounds(80, 5, 100, 20);

        labelPort.setText("Port:");
        pConfig.add(labelPort);
        labelPort.setBounds(205, 5, 50, 20);

        textPort.setColumns(5);
        textPort.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                textPortFocusLost(evt);
            }
        });
        pConfig.add(textPort);
        textPort.setBounds(240, 5, 50, 20);

        bConfig.setBackground(new java.awt.Color(182, 243, 253));
        bConfig.setText("Configuration");
        bConfig.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bConfigActionPerformed(evt);
            }
        });

        bRegistre.setBackground(new java.awt.Color(182, 243, 253));
        bRegistre.setText("Registre");
        bRegistre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bRegistreActionPerformed(evt);
            }
        });

        bLogin.setBackground(new java.awt.Color(226, 243, 253));
        bLogin.setText("Login");
        bLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bLoginActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 323, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(pLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(bRegistre, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(40, 40, 40)
                .addComponent(bConfig, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addComponent(bLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(5, 5, 5)
                .addComponent(pConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(pTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(12, 12, 12)
                .addComponent(pLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(bRegistre, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bConfig, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(bLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addComponent(pConfig, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bLoginActionPerformed
        // TODO add your handling code here:
        String id = this.textId.getText();
        String pwd = new String(this.textPwd.getPassword());
	if(id.trim().equals("") || pwd.trim().equals(""))
        {
          JOptionPane.showMessageDialog(this, "Type your information", "enter interdite",
						JOptionPane.WARNING_MESSAGE);            
        }else
        {
           if(this.isConnect())
           {   
                try{
                clientOd=new ClientOdImpl();
                Login login =new Login(id,pwd);
                System.out.println(id);
                System.out.println(pwd);
                int flag=zEx.login(login, clientOd);
                if(flag==1)
                {
                     System.out.println("Login succesful");
                     Fclient fclient = new Fclient(id,this.zEx,this.clientOd);
			fclient.setVisible(true);
			//fclient.receiveOfflineMessage();
			this.dispose();
                }
                else
                {
                   if(flag==-1)
                   {
                      this.clientOd=null;
                     JOptionPane.showMessageDialog(this, "Problem of server",
				"Login  fail", JOptionPane.ERROR_MESSAGE);
                   }
                   else
                   {
                       if(flag==0)
                       {  
                           this.clientOd=null;
                          JOptionPane.showMessageDialog(this, "Problem : a user has been logined",
				"Login  fail", JOptionPane.ERROR_MESSAGE);
                       }
                       else
                       {
                           this.clientOd=null;
                           JOptionPane.showMessageDialog(this, "Login information incorrect",
				"Login  fail", JOptionPane.ERROR_MESSAGE);
                       }    
                   
                   }    
                
                }    
                
                }
                catch(Exception e)
                {
                  e.printStackTrace();
                }
               
                
                
           }
           else
           {
                JOptionPane.showMessageDialog(this, "Can't connect the server,problem of configuration",
				"Connect fail", JOptionPane.ERROR_MESSAGE);
           }
        
        }    
       
    }//GEN-LAST:event_bLoginActionPerformed

    private void bRegistreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bRegistreActionPerformed
        // TODO add your handling code here:
        if(this.isConnect())
        {
           Fregister fregister =new Fregister(zEx);
           fregister.setVisible(true);
        }
        else
        {
          JOptionPane.showMessageDialog(this, "Can't connect the server,problem of configuration",
				"Connect fail", JOptionPane.ERROR_MESSAGE);
        }    
       
    }//GEN-LAST:event_bRegistreActionPerformed

    private void bConfigActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bConfigActionPerformed
        // TODO add your handling code here:
        if(this.pConfig.isVisible())
        {
           this.pConfig.setVisible(false);
           this.validateTree();
        }
        else
        {
           this.pConfig.setVisible(true);
           this.validateTree();
        }    
    }//GEN-LAST:event_bConfigActionPerformed

    private void textIpFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textIpFocusLost
        // TODO add your handling code here:
        String tmpIp=this.textIp.getText();
        if(tmpIp.matches("\\d+\\.\\d+\\.\\d+\\.\\d+"))
        {
            client.setHostName(tmpIp);
        }
        else
        {
             JOptionPane.showMessageDialog(this, "the format of ip is not right",
				"Format ip", JOptionPane.ERROR_MESSAGE);
        }
            
    }//GEN-LAST:event_textIpFocusLost

    private void textPortFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_textPortFocusLost
        // TODO add your handling code here:
          String tmpPort=this.textPort.getText();
        if(tmpPort.matches("\\d{1,5}"))
        {
            client.setPort(tmpPort);
        }
        else
        {
             JOptionPane.showMessageDialog(this, "the format of ip is not right",
				"Format ip", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_textPortFocusLost

    
    private void setCenter() {
		Dimension sizeScreen = Toolkit.getDefaultToolkit().getScreenSize();
		Dimension sizeAppli = getSize();
		setLocation((sizeScreen.width - sizeAppli.width) / 2,
				(sizeScreen.height - sizeAppli.height) / 2);
	}
   public boolean isConnect()
  {
      if(this.zEx!=null && client.isConnect())
      {
         return true;
      }
      else
      {
        this.zEx=client.run();
        if(this.zEx==null)
        {
         return false;
        }
        else
        {
          return true;
        }    
      }    
  
  }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Flogin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bConfig;
    private javax.swing.JButton bLogin;
    private javax.swing.JButton bRegistre;
    private javax.swing.JLabel labelId;
    private javax.swing.JLabel labelPort;
    private javax.swing.JLabel labelPwd;
    private javax.swing.JLabel labelServer;
    private javax.swing.JLabel labelTitle;
    private javax.swing.JPanel pConfig;
    private javax.swing.JPanel pLogin;
    private javax.swing.JPanel pTitle;
    private javax.swing.JTextField textId;
    private javax.swing.JTextField textIp;
    private javax.swing.JTextField textPort;
    private javax.swing.JPasswordField textPwd;
    // End of variables declaration//GEN-END:variables

private _ZoneExchange  zEx = null;
private ClientOdImpl clientOd =null;
private Client client= null;






}
